package com.sl.springboot.controller;

import com.sl.springboot.model.Card;
import com.sl.springboot.model.Ucard;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.model.User;
import com.sl.springboot.service.CardService;
import com.sl.springboot.service.UcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class UcardController {

    // 依赖注入
    @Autowired
    private UcardService ucardService;
    @Autowired
    private CardService cardService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findUcard")
    public String findUcard(Integer pageIndex, Integer pageSize, Model model,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        String type = (String)session.getAttribute("type");
        User u = (User)session.getAttribute("ad");
        if(type.equals("02")){
            mp.put("uid",u.getId());
        }
        PageInfo<Ucard> pageList = ucardService.findPageInfo(pageIndex,pageSize,mp);
        for(int i = 0;i<pageList.getList().size();i++){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowDate_String = sdf.format(new Date());
            Date nowDate_Date = sdf.parse(nowDate_String);
            Date endDate_Date = sdf.parse(pageList.getList().get(i).getTime());
            int compareTo = nowDate_Date.compareTo(endDate_Date);
            if(compareTo == -1){
                pageList.getList().get(i).setTime2("未到期");
            }else{
                pageList.getList().get(i).setTime2("已到期");
            }
        }
        model.addAttribute("pageList",pageList);
        return "UcardList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addUcard" ,method = RequestMethod.POST)
    @ResponseBody
    public String addUcard( @RequestBody Ucard ucard) {
        int d = ucardService.addUcard(ucard);
        return "UcardList";
    }

    /**
     * 购买
     */
    @RequestMapping( "/addCardPost")
    @ResponseBody
    public String addCardPost(String id,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "201";
        }
        User u = (User)session.getAttribute("ad");
        Map mp = new HashMap();
        mp.put("uid",u.getId());
        mp.put("kid",id);
        List<Ucard> ucards = ucardService.queryFilter(mp);
        for(Ucard ucard:ucards){
            ucardService.deleteUcard(ucard.getId());
        }
//        if(ucards.size()>0){
//            return "已购买过该充值卡，不能重复购买！";
//        }

        Ucard ucard = new Ucard();
        ucard.setKid(id);
        ucard.setUid(u.getId());
        ucard.setMoney("0");
        //推算到期时间
        Card cardById = cardService.findCardById(id);
        int t=0;
        if(cardById.getCardType().equals("周卡")){
            t=7;
        }else if(cardById.getCardType().equals("月卡")){
            t=30;
        }else if(cardById.getCardType().equals("季卡")){
            t=90;
        }else if(cardById.getCardType().equals("年卡")){
            t=365;
        }
        //对当前时间进行转换 括号里面的是转换规则("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowDate = format.format(date);
        date.setTime(System.currentTimeMillis());
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //传参数 就是说要获取几天后的当前具体时间
        c.add(Calendar.DATE, t);
        Date d3 = c.getTime();
        String time = format.format(d3);
        ucard.setTime(time);
        ucard.setDataDate(nowDate);
        int d = ucardService.addUcard(ucard);
        return "200";
    }



    /**
     * 充值
     */
    @RequestMapping(value = "/updateMoney" ,method = RequestMethod.POST)
    @ResponseBody
    public String updateMoney( @RequestBody Ucard ucard) {
        Ucard ucardById = ucardService.findUcardById(ucard.getId());
        BigDecimal bd = new BigDecimal(ucardById.getMoney());
        BigDecimal now = new BigDecimal(ucard.getMoney());
        BigDecimal sum =  bd.add(now);
        ucardById.setMoney(sum.toString());
        int d = ucardService.updateUcard(ucardById);
        return "200";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteUcard")
    @ResponseBody
    public String deleteUcard(String id) {
        int d = ucardService.deleteUcard(id);
        return "UcardList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateUcard")
    public String updateUcard( Ucard ucard) {
        int d = ucardService.updateUcard(ucard);
        return "redirect:/findUcard";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findUcardById")
    public String findUcardById(String id,Model model,HttpServletRequest request) {
        Ucard ucard= ucardService.findUcardById(id);
        model.addAttribute("ucard",ucard);
        return "UcardEdit";
    }


}
